
import database.DBKostum;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.KostumBean;
import validation.ValidateForm2;

public class KostumServlet extends HttpServlet {

    //declare values to get form values from jsp page
    String ID_Kostum;
    String NamaKostum;
    String Harga;
    String Kategori;
    String Gambar;
    String Simpan;
    KostumBean bean = new KostumBean();
    ValidateForm2 validateform2 = new ValidateForm2();
    DBKostum dbkostum = new DBKostum();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //get the values from jsp page
            ID_Kostum = request.getParameter("ID_Kostum");
            NamaKostum = request.getParameter("NamaKostum");
            Harga = request.getParameter("Harga");
            Kategori = request.getParameter("Kategori");
            Gambar = request.getParameter("Gambar");
            Simpan = request.getParameter("Simpan");
            
            /*
            out.println (Nama);
            out.println (Alamat);
            out.println (NoTelp);
            out.println (JenisKelamin);
            out.println (Email);
            out.println (Username);
            out.println (Password);
            */
            
            if (Simpan.equals("Simpan")) {
                //set values to bean.For this call below method
                setValuesToBean();

                //check all form values are valid or not. send bean object 
                KostumBean checkedbean = validateform2.validateData2(bean);
                if (!checkedbean.getIsValid()) {

                    //if data is invalid.set bean object in request and pass that request to
                    //insertupdate.jsp using forward
                    checkedbean.setSimpan("Simpan");
                    request.setAttribute("error", checkedbean);

                    RequestDispatcher rd = request.getRequestDispatcher("entrikostum.jsp");
                    rd.forward(request, response);
                    //now display errors in that jsp page
                } else {
                    //using DBClass object call insertDetails method and pass bean object
                    dbkostum.insertKostum(bean);
                    List list = dbkostum.getAlldetailsKostum();
                    request.setAttribute("list", list);
                    //forward to insertupdate page using requestdispatcher
                    RequestDispatcher rd = request.getRequestDispatcher("viewkostum.jsp");
                    //display a message to client.store message in request object
                    //forwarding to jsp
                    rd.forward(request, response);


                }
            }
            if (Simpan.equals("Edit")) {
                //get userdetails of particular name
                KostumBean kbean = dbkostum.getDetailsKostum(ID_Kostum);
                request.setAttribute("updatekostum", kbean);
                RequestDispatcher rd = request.getRequestDispatcher("entrikostum.jsp");
                rd.forward(request, response);
            }
                       
            if (Simpan.equals("Update")) {
                setValuesToBean();
                KostumBean checkedbean = validateform2.validateData2(bean);
                System.out.println(checkedbean.getIsValid());
                if (!checkedbean.getIsValid()) {

                    //if data is invalid.set bean object in request and pass that request to
                    //insertupdate.jsp using forward
                    checkedbean.setSimpan("Update");
                    request.setAttribute("error", checkedbean);
                    RequestDispatcher rd = request.getRequestDispatcher("entrikostum.jsp");
                    rd.forward(request, response);
                    //now display errors in that jsp page
                } else {
                    //using DBClass object call insertDetails method and pass bean object
                    dbkostum.UpateDetailsKostum(bean, ID_Kostum);
                    List list = dbkostum.getAlldetailsKostum();
                    request.setAttribute("list", list);
                    //forward to insertupdate page using requestdispatcher
                    RequestDispatcher rd = request.getRequestDispatcher("viewkostum.jsp");
                    //display a message to client.store message in request object
                    //forwarding to jsp
                    rd.forward(request, response);


                }

            }
            if (Simpan.equals("Delete")) {
                //delete userdetails of particular name
                dbkostum.deleteDetailsKostum(ID_Kostum);
                List list = dbkostum.getAlldetailsKostum();
                request.setAttribute("list", list);
                RequestDispatcher rd = request.getRequestDispatcher("viewkostum.jsp");
                rd.forward(request, response);
            }
                
        } catch (Exception e) {
            out.println(e);
        } finally {
            out.close();
        }
    }
    //this method is used to setvalues to bean

    public void setValuesToBean() {
        bean.setID_Kostum(ID_Kostum);
        bean.setNamaKostum(NamaKostum);
        bean.setHarga(Harga);
        bean.setKategori(Kategori);
        bean.setGambar(Gambar);
        
        }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }
}